package com.foredu.fiqs.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.foredu.fiqs.domain.Role;
import com.foredu.fiqs.service.IManagementService;
import com.foredu.fiqs.web.actionform.RoleActionForm;

public class RoleAction extends AbstractBaseDispatchAction {

	private IManagementService managementService = null;

	public IManagementService getManagementService() {
		return managementService;
	}

	public void setManagementService(IManagementService managementService) {
		this.managementService = managementService;
	}

	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List<Role> roles = managementService.findRoles();
		request.setAttribute("roles", roles);
		return mapping.findForward("list");
	}

	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return mapping.findForward("add");
	}

	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		RoleActionForm raf = (RoleActionForm) form;
		managementService.deleteRoleById(raf.getId());
		List<Role> roles = managementService.findRoles();
		request.setAttribute("roles", roles);
		return mapping.findForward("delete");
	}

	public ActionForward toUpdate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		RoleActionForm raf = (RoleActionForm) form;
		Role role = managementService.findRoleById(raf.getId());
		raf.setId(role.getId());
		raf.setName(role.getName());
		raf.setDescription(role.getDescription());
		request.setAttribute("role", role);
		return mapping.findForward("toUpdate");
	}

	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		RoleActionForm raf = (RoleActionForm) form;
		Role role = new Role();
		role.setName(raf.getName());
		role.setDescription(raf.getDescription());
		managementService.saveRole(role);
		List<Role> roles = managementService.findRoles();
		request.setAttribute("roles", roles);
		return mapping.findForward("save");
	}

	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		RoleActionForm raf = (RoleActionForm) form;
		Role role = managementService.findRoleById(raf.getId());
		role.setName(raf.getName());
		role.setDescription(raf.getDescription());
		managementService.updateRole(role);

		List<Role> roles = managementService.findRoles();
		request.setAttribute("roles", roles);
		
		return mapping.findForward("update");
	}

}
